package cn.iocoder.yudao.module.system.controller.app.suggestion;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.suggestion.SuggestionDO;
import cn.iocoder.yudao.module.system.service.suggestion.SuggestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 意见建议")
@RestController
@RequestMapping("/system/suggestion")
@Validated
public class AppSuggestionController {

    @Resource
    private SuggestionService suggestionService;

    @PostMapping("/create")
    @Operation(summary = "创建意见建议")
    public CommonResult<Long> createSuggestion(@Valid @RequestBody SuggestionSaveReqVO createReqVO) {
        return success(suggestionService.createSuggestion(getLoginUserId(), createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新意见建议")
    public CommonResult<Boolean> updateSuggestion(@Valid @RequestBody SuggestionSaveReqVO updateReqVO) {
        suggestionService.updateSuggestion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除意见建议")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSuggestion(@RequestParam("id") Long id) {
        suggestionService.deleteSuggestion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得意见建议")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SuggestionRespVO> getSuggestion(@RequestParam("id") Long id) {
        SuggestionDO suggestion = suggestionService.getSuggestion(id);
        return success(BeanUtils.toBean(suggestion, SuggestionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得意见建议分页")
    public CommonResult<PageResult<SuggestionRespVO>> getSuggestionPage(@Valid SuggestionPageReqVO pageReqVO) {
        PageResult<SuggestionDO> pageResult = suggestionService.getSuggestionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SuggestionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出意见建议 Excel")
    @OperateLog(type = EXPORT)
    public void exportSuggestionExcel(@Valid SuggestionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SuggestionDO> list = suggestionService.getSuggestionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "意见建议.xls", "数据", SuggestionRespVO.class,
                        BeanUtils.toBean(list, SuggestionRespVO.class));
    }

}